<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/component/pear/css/pear.css"/>
    <script src="/static/component/layui/layui.js"></script>
    <script src="/static/component/pear/pear.js"></script>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">

                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="username" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="dataTable" lay-filter="dataTable"></table>
    </div>
</div>

<script type="text/html" id="toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>


<script type="text/html" id="options">
    <button class="pear-btn pear-btn-primary pear-btn-xs" lay-event="edit">编辑</button>
    <button class="pear-btn pear-btn-danger pear-btn-xs" lay-event="remove">删除</button>
    <button class="pear-btn pear-btn-xs" lay-event="info">基本信息</button>
    <button class="pear-btn pear-btn-xs" lay-event="more" id="more">更多 <i class="layui-icon layui-icon-down"></i>
    </button>
</script>
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'laydate', 'dropdown'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let laydate = layui.laydate;
        let dropdown = layui.dropdown;
        let MODULE_PATH = "{$Request.root}/study.student_list/";

        let cols = [
            [{
                field: "id",
                title: "#",
                unresize: "true",
                align: "center"
            }, {
                field: "username",
                title: "姓名",
                unresize: "true",
                align: "center"
            },
            //     {
            //     field: "birthday",
            //     title: "生日",
            //     unresize: "true",
            //     align: "center"
            // },
                {
                field: "sex",
                title: "性别",
                unresize: "true",
                align: "center"
            }, {
                field: "student_img",
                title: "学生照片",
                unresize: "true",
                align: "center",
                templet: function (d) {
                    let url = d.student_img;
                    return '<img  src="' + url + '" class="tb-img-circle" alt=""/>';
                }
            }, {
                field: "class_id",
                title: "班级",
                unresize: "true",
                align: "center", templet: d => {
                    return d.get_class ? d.get_class.class_name : '未分班'
                }
            }, {
                field: "end_time",
                title: "到期时间",
                unresize: "true",
                align: "center",
                // templet: d => {
                //     var date = new Date(d.end_time * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
                //     let year = date.getFullYear(),
                //         month = ("0" + (date.getMonth() + 1)).slice(-2),
                //         sdate = ("0" + date.getDate()).slice(-2)
                //     // 拼接
                //     return year + "-" + month + "-" + sdate
                // }
            }, {
                field: "mobile",
                title: "联系方式",
                unresize: "true",
                align: "center"
            }, {
                title: '操作',
                toolbar: '#options',
                unresize: true,
                align: 'center',
                width: 300,
            }
            ]
        ]

        table.render({
            elem: '#dataTable',
            url: MODULE_PATH + 'index',
            page: true,
            cols: cols,
            cellMinWidth: 100,
            skin: 'line',
            toolbar: '#toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('tool(dataTable)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if ('info' === obj.event) {
                window.show_info(obj);
            } else if ('more' === obj.event) {
                window.more_info(obj);
            }
        });

        table.on('toolbar(dataTable)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            } else if (obj.event === 'recycle') {
                window.recycle(obj);
            }
        });

        form.on('submit(query)', function (data) {
            table.reload('dataTable', {
                where: data.field,
                page: {curr: 1}
            })

            return false;
        });

        //弹出窗设置 自己设置弹出百分比
        function screen() {
            if (typeof width !== 'number' || width === 0) {
                width = $(window).width() * 0.9;
            }
            if (typeof height !== 'number' || height === 0) {
                height = $(window).height() - 20;
            }
            return [width + 'px', height + 'px'];
        }

        window.add = function () {
            layer.open({
                type: 2,
                maxmin: true,
                title: '新增学生管理',
                shade: 0.1,
                area: screen(),
                content: MODULE_PATH + 'add'
            });
        }

        window.more_info = function (obj) {
            //更多下拉菜单
            dropdown.render({
                elem: '#more'
                , show: true //外部事件触发即显示
                , data: [{
                    title: '今日事件'
                    , id: 'a'
                }, {
                    title: '出勤记录'
                    , id: 'b'
                }, {
                    title: '成长数据'
                    , id: 'c'
                }, {
                    title: "疫苗记录",
                    id: 'd'}, {
                    title: "发展报告",
                    id: 'e'}
                ]
                , click: function (data, othis) {
                    //根据 id 做出不同操作
                    if (data.id === 'a') {
                        layer.open({
                            type: 2,
                            maxmin: true,
                            title: '今日事件',
                            shade: 0.1,
                            area: screen(),
                            content: MODULE_PATH + 'action/id/' + obj.data['id']
                        });
                    } else if (data.id === 'b') {
                        layer.open({
                            type: 2,
                            maxmin: true,
                            title: '考勤事件',
                            shade: 0.1,
                            area: screen(),
                            content: MODULE_PATH + 'attendance/id/' + obj.data['id']
                        });
                    } else if (data.id === 'c') {
                        layer.open({
                            type: 2,
                            maxmin: true,
                            title: '考勤事件',
                            shade: 0.1,
                            area: screen(),
                            content: MODULE_PATH + 'student_body/id/' + obj.data['id']
                        });
                    } else if (data.id === 'd') {
                        layer.open({
                            type: 2,
                            maxmin: true,
                            title: '疫苗',
                            shade: 0.1,
                            area: screen(),
                            content: MODULE_PATH + 'vaccines/id/' + obj.data['id']
                        });
                    } else if ('e' === data.id) {
                        layer.open({
                            type: 2,
                            maxmin: true,
                            title: '发展报告',
                            shade: 0.1,
                            area: screen(),
                            content: "{$Request.root}/study.student_report/" + 'index/id/' + obj.data['id']
                        });
                    }

                }
                , align: 'right' //右对齐弹出（v2.6.8 新增）
                , style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' //设置额外样式
            });
        }


        window.show_info = function (obj) {
            layer.open({
                type: 2,
                maxmin: true,
                title: '学生详情',
                shade: 0.1,
                area: screen(),
                content: MODULE_PATH + 'studentInfo/id/' + obj.data['id']
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                maxmin: true,
                title: '修改学生管理',
                shade: 0.1,
                area: screen(),
                content: MODULE_PATH + 'edit/id/' + obj.data['id']
            });
        }

        window.recycle = function () {
            layer.open({
                type: 2,
                maxmin: true,
                title: '回收站',
                shade: 0.1,
                area: screen(),
                content: MODULE_PATH + 'recycle',
                cancel: function () {
                    table.reload('dataTable');
                }
            });
        }


        window.remove = function (obj) {
            layer.confirm('确定要删除该学生管理', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + 'remove',
                    data: {id: obj.data['id']},
                    dataType: 'json',
                    type: 'POST',
                    success: function (res) {
                        layer.close(loading);
                        //判断有没有权限
                        if (res && res.code == 999) {
                            layer.msg(res.msg, {
                                icon: 5,
                                time: 2000,
                            })
                            return false;
                        } else if (res.code == 200) {
                            layer.msg(res.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(res.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }
            var ids = []
            var hasCheck = table.checkStatus('dataTable')
            var hasCheckData = hasCheck.data
            if (hasCheckData.length > 0) {
                $.each(hasCheckData, function (index, element) {
                    ids.push(element.id)
                })
            }
            layer.confirm('确定要删除这些学生管理', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + 'batchRemove',
                    data: {ids: ids},
                    dataType: 'json',
                    type: 'POST',
                    success: function (res) {
                        layer.close(loading);
                        //判断有没有权限
                        if (res && res.code == 999) {
                            layer.msg(res.msg, {
                                icon: 5,
                                time: 2000,
                            })
                            return false;
                        } else if (res.code == 200) {
                            layer.msg(res.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('dataTable');
                            });
                        } else {
                            layer.msg(res.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('dataTable');
        }
    })
</script>
</body>
</html>
